// detail/mwhere/mwhere.js
Page({

  //页面的初始数据
  data:{
    id: wx.getStorageSync('userid'),
    img:wx.getStorageSync('userimg'),
    name:wx.getStorageSync('username'),
    sayslist:[]
  },

  //点赞
  like:function(e){
    //获取所点动态信息
    let sayid=e.currentTarget.dataset.id
    let like =e.currentTarget.dataset.like
    let img  =e.currentTarget.dataset.img
    let num  =e.currentTarget.dataset.num
    let likeid=e.currentTarget.dataset.likeid

    console.log(sayid,like,img,num)
    if(like=="true"){ //取消点赞     
      like="false"
      --num
      img="/icon/like.png"
    }else{ //点赞
      like="true"
      ++num
      img="/icon/like1.png"
    }

    //更改当前动态点赞数量信息
    wx.request({
      url: `https://www.prebeauty.work:3001/api/says/${sayid}`,
      data:{
        likenum:num
      },
      header: {
        'content-type': 'application/json'
      },
      method: 'PUT',
      success:(res)=>{

      }
    })
    //更改当前动态用户点赞信息
    wx.request({
      url: `https://www.prebeauty.work:3001/api/like/${likeid}`,
      data:{
        sayid:sayid,
        userid:this.data.id,
        islike:like,
        likeimg:img
      },
      header: {
        'content-type': 'application/json'
      },
      method: 'PUT',
      success:(res)=>{

        //更改本地 当前动态点赞信息
        let saysdata=this.data.sayslist
        for(let i=0;i<saysdata.length;i++){
          if(saysdata[i].sayid==sayid){
            saysdata[i].islike=like
            saysdata[i].likenum=num
            saysdata[i].likeimg=img
            this.setData({
              sayslist:saysdata
            })
          }
        }
      }
    })
     
  },

  //关注
  attention:function(e){
    console.log(e)
    const sayid=e.currentTarget.dataset.sayid//动态id
    let attention=e.currentTarget.dataset.attention//是否关注
    const attentionid=e.currentTarget.dataset.attentionid//被点击者id

    
    let method=''
    if(attention=="已关注"){//取消关注
      method='DELETE'
      attention="关注"
      wx.request({
        url: `https://www.prebeauty.work:3001/api/attention/${this.data.id}`,
        data:{
          attentionid:attentionid
        },
        header: {
          'content-type': 'application/json'
        },
        method: method,
        success:(res)=>{
          console.log("attention click success!")
        }
      })
    }else{//关注
      method='POST'
      attention="已关注"
      wx.request({
        url: "https://www.prebeauty.work:3001/api/attention",
        data:{
          userid:this.data.id,
          attentionid:attentionid
        },
        header: {
          'content-type': 'application/json'
        },
        method: method,
        success:(res)=>{
          console.log("attention click success!")
        }
      })
    }
    

    //将本地的attention进行更新
    let says=this.data.sayslist
    for(let i=0;i<says.length;i++){
      if(says[i].userid==attentionid){
        says[i].attention=attention
      }
    }
    this.setData({
      sayslist:says
    })
    console.log(this.data.sayslist)
  },


  // 生命周期函数--监听页面加载
  onLoad: function (options) {
    
  },

  //生命周期函数--监听页面初次渲染完成
  onReady: function () {
    
  },

  // 生命周期函数--监听页面显示
  onShow: function () {
    //判断登录
    if(!wx.getStorageSync('token')){
      wx.showModal({
        title: '用户未登录',
        content: '请先进行授权登录',
        success(res) {
          if (res.confirm) {
            console.log('确定')
            wx.switchTab({
              url: '/pages/home/home'
           })
          } else if (res.cancel) {
            console.log('取消')
            wx.switchTab({
              url: '/pages/index/index'
           })
          }
        }
      })
    }

    const that=this

    //获取所有用户所有动态
    wx.request({
      url: 'https://www.prebeauty.work:3001/api/says',
      data:{
        groupid:1
      },
      header: {
        'content-type': 'application/json'
      },
      method: 'GET',
      success:async (res)=>{
        console.log(res.data)
        const says=res.data
        for(let i=0;i<says.length;i++){

          //图片 字符串->数组
          if(says[i].sayimg){
            let sayimgs=says[i].sayimg.slice(1,-1)
            sayimgs=sayimgs.split(',')
            
            for(let j=0;j<sayimgs.length;j++){
              sayimgs[j]=sayimgs[j].slice(1,-1)
              sayimgs[j]="https://www.prebeauty.work:3001/static/image/"+sayimgs[j]
            }
            says[i].sayimg=sayimgs
          }
          console.log(says[i].userid)

          //判断是否点赞 初始点赞数
          await new Promise((rv,rj)=>{
            wx.request({
              url: `https://www.prebeauty.work:3001/api/like?userid=${this.data.id}&sayid=${says[i].sayid}`,
              header: {
                'content-type': 'application/json'
              },
              method: 'GET',
              success:(res)=>{
                says[i].likeid=res.data[0].id
                says[i].islike=res.data[0].islike
                says[i].likeimg=res.data[0].likeimg
                rv()
              },
              fail : err => {
                rv()
              }
            })
          })

          //头像 和 用户名
          await new Promise((rv,rj) => {
            wx.request({
              url: `https://www.prebeauty.work:3001/api/userpage?userid=${says[i].userid}`,
              header: {
                'content-type': 'application/json'
              },
              method: 'GET',
              success:(res)=>{
                console.log(res.data)
                says[i].username=res.data[0].username
                says[i].userimg=res.data[0].userimg

                //获取用户关注
                wx.request({
                  url: `https://www.prebeauty.work:3001/api/attention?userid=${this.data.id}&attentionid=${says[i].userid}`,
                  header: {
                    'content-type': 'application/json'
                  },
                  method: 'GET',
                  success:(res)=>{
                    console.log(res.data)
                    says[i].attention=res.data?"已关注":"关注"
                    rv()
                  },
                  fail : err => {
                    rv()
                  }
                })
              },
            })
          })
        }

        that.setData({
          sayslist:says
        }) 
        console.log(this.data.sayslist)
      }
    })
  },

  //生命周期函数--监听页面隐藏
  onHide: function () {

  },

  //生命周期函数--监听页面卸载
  onUnload: function () {

  },

  //页面相关事件处理函数--监听用户下拉动作

  onPullDownRefresh: function () {

  },

  //页面上拉触底事件的处理函数
  onReachBottom: function () {

  },

  //用户点击右上角分享
  onShareAppMessage: function () {

  }
})